package com.ruoyi.web.controller.business.sundry;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.sundry.domain.JbRawMaterialAudit;
import com.ruoyi.business.sundry.domain.JbRawMaterialAuditRecord;
import com.ruoyi.business.sundry.domain.JbRawMaterialPrice;
import com.ruoyi.business.sundry.domain.vo.JbMaterialPriceImportAdd;
import com.ruoyi.business.sundry.service.JbRawMaterialAuditRecordService;
import com.ruoyi.business.sundry.service.JbRawMaterialAuditService;
import com.ruoyi.business.sundry.service.JbRawMaterialPriceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 原材料价格 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("原材料价格")
@RestController
@RequestMapping("/raw_material_price")
public class JbRawMaterialPriceController extends BaseController
{
    @Autowired
    private JbRawMaterialPriceService service;
    @Autowired
    private JbRawMaterialAuditService jbRawMaterialAuditService;

    /**
     * 查询原材料价格列表
     */
    @ApiOperation("查询原材料价格列表")
    @PreAuthorize("@ss.hasPermi('raw_material_price:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbRawMaterialPrice jbRawMaterialPrice)
    {
        IPage<JbRawMaterialPrice> iPage = service.pageList(page, jbRawMaterialPrice);
        return JoR.getDataTable(iPage);
    }

    /**
     * 导出原材料价格列表
     */
    @ApiOperation("导出原材料价格列表")
    @PreAuthorize("@ss.hasPermi('raw_material_price:export')")
    @Log(title = "原材料价格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbRawMaterialPrice jbRawMaterialPrice)
    {
        LambdaQueryWrapper<JbRawMaterialPrice> select = new LambdaQueryWrapper<>();
        IPage<JbRawMaterialPrice> iPage = service.page(page.b(), select);

        List<JbRawMaterialPrice> list = iPage.getRecords();
        ExcelUtil<JbRawMaterialPrice> util = new ExcelUtil<JbRawMaterialPrice>(JbRawMaterialPrice.class);
        util.exportExcel(response, list, "原材料价格数据");
    }

    /**
     * 获取原材料价格详细信息
     */
    @ApiOperation("获取原材料价格详细信息")
    @PreAuthorize("@ss.hasPermi('raw_material_price:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增原材料价格
     */
    @ApiOperation("新增原材料价格")
    @PreAuthorize("@ss.hasPermi('raw_material_price:add')")
    @Log(title = "原材料价格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbRawMaterialPrice jbRawMaterialPrice)
    {
        long count = service.count(Wrappers.lambdaQuery(JbRawMaterialPrice.class).eq(JbRawMaterialPrice::getMaterialId, jbRawMaterialPrice.getMaterialId()));
        if (count>0l)
        {
            return AjaxResult.error("原材料ID已存在");
        }
        return toAjax(service.saveInfo(jbRawMaterialPrice));
    }

    /**
     * 修改原材料价格
     */
    @ApiOperation("修改原材料价格")
    @PreAuthorize("@ss.hasPermi('raw_material_price:edit')")
    @Log(title = "原材料价格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbRawMaterialPrice jbRawMaterialPrice)
    {

        return toAjax(service.updateInfo(jbRawMaterialPrice));
    }

    /**
     * 删除原材料价格
     */
    @ApiOperation("删除原材料价格")
    @PreAuthorize("@ss.hasPermi('raw_material_price:remove')")
    @Log(title = "原材料价格", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }

    @Log(title = "原材料导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('raw_material_price:export')")
    @PostMapping("/importAdd")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<JbMaterialPriceImportAdd> util = new ExcelUtil<>(JbMaterialPriceImportAdd.class);
        List<JbMaterialPriceImportAdd> opportList = util.importExcel(file.getInputStream());
        boolean b = false;
        if (ObjectUtils.isNotEmpty(opportList))
        {
            b = service.importData(opportList);
        }
        return toAjax(b);
    }
    @Log(title = "批量更新模版下载", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('raw_material_price:export')")
    @PostMapping("/templateDownload")
    public void importData(HttpServletResponse response, PageParam page, JbRawMaterialPrice jbRawMaterialPrice)
    {
//        LambdaQueryWrapper<JbRawMaterialPrice> select = new LambdaQueryWrapper<>();
//        IPage<JbRawMaterialPrice> iPage = service.page(page.b(), select);
//        List<JbRawMaterialPrice> list = iPage.getRecords();
//        List<JbRawMaterialPrice> listData = service.list();
//        List<JbMaterialPriceImportAdd> list=BeanUtil.copyToList(listData,JbMaterialPriceImportAdd.class);
        List<JbMaterialPriceImportAdd> list= Lists.newArrayList();
        ExcelUtil<JbMaterialPriceImportAdd> util = new ExcelUtil<JbMaterialPriceImportAdd>(JbMaterialPriceImportAdd.class);
        util.exportExcel(response, list, "模版数据导出");
    }
    @Log(title = "原材料批量更新导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('raw_material_price:export')")
    @PostMapping("/batchImportUpdate")
    public AjaxResult batchImportUpdate(MultipartFile file) throws Exception
    {
        ExcelUtil<JbMaterialPriceImportAdd> util = new ExcelUtil<>(JbMaterialPriceImportAdd.class);
        List<JbMaterialPriceImportAdd> opportList = util.importExcel(file.getInputStream());
        boolean b = false;
        if (ObjectUtils.isNotEmpty(opportList))
        {
            b=service.batchUpdateInfo(opportList);
        }
        return toAjax(b);
    }
    /**
     * 查询原材料价格审核列表
     */
    @ApiOperation("查询原材料价格审核列表")
    @PreAuthorize("@ss.hasPermi('raw_material_price:auditList')")
    @GetMapping("/auditList")
    public TableDataInfo auditList(PageParam page, JbRawMaterialAudit jbRawMaterialAudit)
    {
        IPage<JbRawMaterialAudit> iPage = service.auditList(page, jbRawMaterialAudit);
        return JoR.getDataTable(iPage);
    }
    /**
     * 查询原材料价格审核详情
     */
    @ApiOperation("查询原材料价格审核详情")
    @PreAuthorize("@ss.hasPermi('raw_material_price:auditDetails')")
    @GetMapping("/auditDetails/{id}")
    public AjaxResult auditlist(@PathVariable("id") Long id)
    {
        return success(jbRawMaterialAuditService.getById(id));
    }
    /**
     * 查询原材料价格审核列表
     */
    @ApiOperation("查询原材料价格审核列表")
    @PreAuthorize("@ss.hasPermi('raw_material_price:auditRecrodList')")
    @GetMapping("/auditRecrodList")
    public AjaxResult auditRecrodList(PageParam page, JbRawMaterialAuditRecord jbRawMaterialAuditRecord)
    {
        List<JbRawMaterialAuditRecord> list = service.auditRecrodList(page, jbRawMaterialAuditRecord);
        return success(list);
    }
    /**
     * 修改审核记录的状态
     */
    @Log(title = "修改审核记录的状态", businessType = BusinessType.UPDATE)
    @ApiOperation("修改审核记录的状态")
    @PreAuthorize("@ss.hasPermi('raw_material_price:updateAuditStatus')")
    @PutMapping("/updateAuditRecrodStatus")
    public AjaxResult updateAuditStatus(@RequestBody JbRawMaterialAudit JbRawMaterialAudit)
    {
        return toAjax(service.updateAuditStatus(JbRawMaterialAudit));
    }

}
